package org.mat.admin.sys.controller;


import org.mat.admin.sys.api.domain.SysDictType;
import org.mat.admin.sys.controller.page.TableDataInfo;
import org.mat.admin.sys.service.ISysDictTypeService;
import org.mat.admin.sys.util.SecurityUtils;
import org.mat.framework.lang.constant.DefaultErrors;
import org.mat.framework.lang.dto.MatApiResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 数据字典信息
 * 
 * @author ruoyi
 */
@RestController
@RequestMapping("/dict/type")
public class SysDictTypeController extends BaseController
{
    @Autowired
    private ISysDictTypeService dictTypeService;


    @GetMapping("/list")
    public TableDataInfo list(SysDictType dictType)
    {
        startPage();
        List<SysDictType> list = dictTypeService.selectDictTypeList(dictType);
        return getDataTable(list);
    }


//    @PostMapping("/export")
//    public void export(HttpServletResponse response, SysDictType dictType)
//    {
//        List<SysDictType> list = dictTypeService.selectDictTypeList(dictType);
//        ExcelUtil<SysDictType> util = new ExcelUtil<SysDictType>(SysDictType.class);
//        util.exportExcel(response, list, "字典类型");
//    }

    /**
     * 查询字典类型详细
     */

    @GetMapping(value = "/{dictId}")
    public MatApiResponse getInfo(@PathVariable Long dictId)
    {
        return MatApiResponse.success(dictTypeService.selectDictTypeById(dictId));
    }

    /**
     * 新增字典类型
     */

    @PostMapping
    public MatApiResponse add(@Validated @RequestBody SysDictType dict)
    {
        if (UserConstants.NOT_UNIQUE.equals(dictTypeService.checkDictTypeUnique(dict)))
        {
            return MatApiResponse.fail(DefaultErrors.DEFAULT.getCode(), "新增字典'" + dict.getDictName() + "'失败，字典类型已存在");
        }
        dict.setCreateBy(SecurityUtils.getUsername());
        return toAjax(dictTypeService.insertDictType(dict));
    }

    /**
     * 修改字典类型
     */

    @PutMapping
    public MatApiResponse edit(@Validated @RequestBody SysDictType dict)
    {
        if (UserConstants.NOT_UNIQUE.equals(dictTypeService.checkDictTypeUnique(dict)))
        {
            return MatApiResponse.fail(DefaultErrors.DEFAULT.getCode(), "修改字典'" + dict.getDictName() + "'失败，字典类型已存在");
        }
        dict.setUpdateBy(SecurityUtils.getUsername());
        return toAjax(dictTypeService.updateDictType(dict));
    }

    /**
     * 删除字典类型
     */

    @DeleteMapping("/{dictIds}")
    public MatApiResponse remove(@PathVariable Long[] dictIds)
    {
        dictTypeService.deleteDictTypeByIds(dictIds);
        return success();
    }

    /**
     * 刷新字典缓存
     */

    @DeleteMapping("/refreshCache")
    public MatApiResponse refreshCache()
    {
        dictTypeService.resetDictCache();
        return MatApiResponse.success(null);
    }

    /**
     * 获取字典选择框列表
     */
    @GetMapping("/optionselect")
    public MatApiResponse optionselect()
    {
        List<SysDictType> dictTypes = dictTypeService.selectDictTypeAll();
        return MatApiResponse.success(dictTypes);
    }
}
